﻿using QLNS_v1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLNS_v1.Bus
{
    public class NhanVien_BTO:NhanVien
    {

        public string NgachLuong { get; set; }
        public string BacLuong { get; set; }
        public string PhuCap { get; set; }
        public double BHYT { get; set; }
        public double BHXH { get; set; }
        public double BHTN { get; set; }
        
        public float LuongCoBan { get; set; }
        public double LuongNhan { get; set; }
        public float NgayCong { get; set; }
        public float NgayNghi { get; set; }
    }
   public class NhanVien_Bus
    {
       NhanVienContextDataContext nhanvien = new NhanVienContextDataContext();
       
        public List<NhanVien_BTO> ListNhanVien()
        {
            var query = (from sv in nhanvien.NhanViens
                         join dt in nhanvien.NgachLuongs on sv.NgachLuongID equals dt.NgachLuongID into dts
                         from dantoc_ in dts.DefaultIfEmpty()
                         join l in nhanvien.BacLuongs on sv.BacLuongID equals l.BacLuongID into bacluongs
                         from lops in bacluongs.DefaultIfEmpty()
                         select new NhanVien_BTO
                         {
                            NhanVienID=sv.NhanVienID,
                            MaNhanVien=sv.MaNhanVien,
                            HoTen=sv.HoTen,
                            NgachLuongID=sv.NgachLuongID,
                            BacLuongID=sv.BacLuongID,
                            PhuCapXang=sv.PhuCapXang,
                            PhuCapChucVu=sv.PhuCapChucVu,
                            PhuCapCom=sv.PhuCapCom,
                            PhuCapChungChi=sv.PhuCapChungChi,
                            PhuCapDocHai=sv.PhuCapDocHai,
                            NgachLuong=dantoc_.TenNgach,
                            BacLuong=lops.TenBac,
                            HSL=sv.HSL,


                            
                            
                         });
            return query.ToList<NhanVien_BTO>();
        }

        public List<NhanVien_BTO> ListNhanVien_Luong(int thang,int nam)
        {
            ChamCong_Bus ccb=new ChamCong_Bus();
            LuongCB_Bus lcb=new LuongCB_Bus();
            List<Option>Listluongcb=lcb.ListLuongCB();
            float luong=0;
            foreach(var a in Listluongcb)
                 luong=float.Parse(a.LuongCoBan.ToString());
            OptionBH bh = new OptionBH();
            OptionBH_Bus bhb = new OptionBH_Bus();
            List<OptionBH> listbh = bhb.ListBH();
            foreach (var a in listbh)
            {
                bh.BHTN = a.BHTN;
                bh.BHXH = a.BHXH;
                bh.BHYT = a.BHYT;
            }
            int maxdate = ccb.SoNgayPhaiDiLam(nam, thang);
            var query = (from sv in nhanvien.NhanViens
                         join dt in nhanvien.ChamCongs on sv.NhanVienID equals dt.NhanVienID into dts
                         from dantoc_ in dts.DefaultIfEmpty()
                         where dantoc_.Nam==nam && dantoc_.Thang==thang 
                         select new NhanVien_BTO
                         {
                             
                             NhanVienID = sv.NhanVienID,
                             MaNhanVien = sv.MaNhanVien,
                             HoTen = sv.HoTen,
                             NgachLuongID = sv.NgachLuongID,
                             BacLuongID = sv.BacLuongID,
                             PhuCapXang = sv.PhuCapXang,
                             PhuCapChucVu = sv.PhuCapChucVu,
                             PhuCapCom = sv.PhuCapCom,
                             PhuCapChungChi = sv.PhuCapChungChi,
                             PhuCapDocHai = sv.PhuCapDocHai,
                             HSL=sv.HSL,
                             LuongCoBan=luong,
                             BHTN=(double)bh.BHTN,
                             BHXH = (double)bh.BHXH,
                             BHYT = (double)bh.BHYT,
                             NgayCong=(float)dantoc_.SoNgayDiLam,
                             NgayNghi = (float)(maxdate - dantoc_.SoNgayDiLam),
                             LuongNhan = (maxdate == dantoc_.SoNgayDiLam ? (double)((luong * sv.HSL) - (luong * sv.HSL * ((bh.BHTN + bh.BHXH + bh.BHYT) / 100.0)) + sv.PhuCapXang + sv.PhuCapDocHai + sv.PhuCapCom + sv.PhuCapChungChi + sv.PhuCapChucVu) : ((double)((luong * sv.HSL) - (luong * sv.HSL * ((bh.BHTN + bh.BHXH + bh.BHYT) / 100.0)) + sv.PhuCapXang + sv.PhuCapDocHai + sv.PhuCapCom + sv.PhuCapChungChi + sv.PhuCapChucVu) / maxdate) * (double)(maxdate - dantoc_.SoNgayDiLam)),
                            
                            
                         });
            return query.ToList<NhanVien_BTO>();
        }
    }
}
